package com.uzai.console.service.remoteapi.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.pyq.GoodsGallery.GoodsGalleryQuery;
import com.uzai.console.service.remoteapi.GoodsGalleryService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.remoteapi.goodsgallery.GoodsGallery;
import com.uzai.console.vo.remoteapi.goodsgallery.GoodsGalleryType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: liuqi
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class GoodsGalleryServiceImpl implements GoodsGalleryService {

    private static Logger logger = LoggerFactory.getLogger(GoodsGalleryServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 分页查询图片信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param goodsGalleryQuery
     * @return null
     */
    public Page<GoodsGallery> findGoodsGalleryByPage(GoodsGalleryQuery goodsGalleryQuery) {
        Page<GoodsGallery> goodsGalleryPage = new Page<>();
        try{
            String cpsadminServerUrl = uzaiConsoleSysConfig.getCpsadminServerUrl();
            String remoteUrl = cpsadminServerUrl + "/gallery/queryImage";
            Map<String, Object> param = new HashMap<>();
            param.put("imageType", goodsGalleryQuery.getImageType());
            param.put("current", goodsGalleryQuery.getCurrent());
            param.put("size", goodsGalleryQuery.getSize());
            param.put("orderByField", goodsGalleryQuery.getOrderByField());
            param.put("isAsc",goodsGalleryQuery.isAsc());
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, headers);

            logger.info("--GoodsGalleryServiceImpl ---findListByPage remoteUrl={}, para={}", remoteUrl, JSONObject.toJSONString(param));

            ResponseEntity<String> response = restTemplate.postForEntity(remoteUrl, request, String.class);
            if (response.getStatusCode() != HttpStatus.OK){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            try {
                String body = response.getBody();
                logger.info(body);
                JSONObject object = JSONObject.parseObject(body);
                JSONObject data = object.getJSONObject("data");
                if(data != null){
                    List<GoodsGallery> goodsGalleryList =  JSONObject.parseArray(data.getString("records"), GoodsGallery.class);
                    goodsGalleryPage.setRecords(goodsGalleryList);
                    goodsGalleryPage.setTotal(data.getLong("total"));
                    goodsGalleryPage.setPages(data.getInteger("pages"));
                }
            }catch (Exception e){
                logger.error("--GoodsGalleryServiceImpl findListByPage request error..{}", e.getMessage());
            }
        }catch (Exception e){
            logger.error("--GoodsGalleryServiceImpl findListByPage request error..{}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }

        return goodsGalleryPage;

    }

    /**
     * 查询图片信息类型列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param
     * @return null
     */
    public List<GoodsGalleryType> findGoodsGalleryTypeByList(){
        List<GoodsGalleryType> goodsGalleryTypeList = new ArrayList<>();
        try{
            String cpsadminServerUrl = uzaiConsoleSysConfig.getCpsadminServerUrl();
            String remoteUrl = cpsadminServerUrl + "/gallery/getImageTypeDropdown";
            Map<String, Object> param = new HashMap<>();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, headers);
            logger.info("--GoodsGalleryServiceImpl ---findGoodsGalleryTypeByList remoteUrl={}, para={}", remoteUrl, JSONObject.toJSONString(param));
            ResponseEntity<String> response = restTemplate.postForEntity(remoteUrl, request, String.class);
            if (response.getStatusCode() != HttpStatus.OK){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            try {
                String body = response.getBody();
                logger.info(body);
                JSONObject object = JSONObject.parseObject(body);
                if(object != null){
                    goodsGalleryTypeList = JSONObject.parseArray(object.getString("data"), GoodsGalleryType.class);
                }
            }catch (Exception e){
                logger.error("--GoodsGalleryServiceImpl findGoodsGalleryTypeByList request error..{}", e.getMessage());
            }
        }catch (Exception e){
            logger.error("--GoodsGalleryServiceImpl findGoodsGalleryTypeByList request error..{}", e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }

        return goodsGalleryTypeList;

    }
}
